<?php

//decode by 012wz.com QQ:800083075
global $_W, $_GPC;
$type = intval($_GPC['type']);
$operation = empty($_GPC['op']) ? 'display' : $_GPC['op'];
if ($operation == 'display') {
    $pindex = max(1, intval($_GPC['page']));
	$psize = 20;
        if (!empty($_POST['realname'])) {
                $condition="";
                $_POST['realname'] = trim($_POST['realname']);
                $condition .= ' and ( m.realname like :realname or m.nickname like :realname)';
                $params[':realname'] = "%{$_POST['realname']}%";
                $list = pdo_fetchAll("SELECT r.*,m.avatar,m.realname,m.mobile,m.weixin,m.company,m.gender,m.province,m.city,m.area,m.nickname  FROM " . tablename('ewei_shop_commission_gold_log') . " r left  join " . tablename('ewei_shop_member') . " m on r.mid=m.id and r.uniacid=m.uniacid" . " WHERE r.mid =m.id  AND r.uniacid=" . $_W['uniacid'] . " $condition order by r.id desc",$params);
                foreach($list as $key => $value){
                    if($value['openid']){
                    $info=pdo_fetch('select * from '.tablename('ewei_shop_member').' where openid=:openid ',array(':openid'=>$value['openid']));
                    $list[$key]['nickname2']=$info['nickname'];
                    $list[$key]['realname2']=$info['realname'];
                    }
                    $list[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                    if($value['gettime']!=0){
                    $list[$key]['gettime'] = date('Y-m-d H:i:s',$value['gettime']);    
                    }else{
                    $list[$key]['gettime']="";   
                    }
                }
        }else{
                $list = pdo_fetchAll("SELECT r.*,m.avatar,m.realname,m.mobile,m.weixin,m.company,m.gender,m.province,m.city,m.area,m.nickname  FROM " . tablename('ewei_shop_commission_gold_log') . " r left  join " . tablename('ewei_shop_member') . " m on r.mid=m.id and r.uniacid=m.uniacid" . " WHERE r.mid =m.id  AND r.uniacid=" . $_W['uniacid'] . " order by r.id desc LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
                foreach($list as $key => $value){
                    if($value['openid']){
                    $info=pdo_fetch('select * from '.tablename('ewei_shop_member').' where openid=:openid ',array(':openid'=>$value['openid']));
                    $list[$key]['nickname2']=$info['nickname'];
                    $list[$key]['realname2']=$info['realname'];
                    }
                    $list[$key]['createtime'] = date('Y-m-d H:i:s',$value['createtime']);
                    if($value['gettime']!=0){
                    $list[$key]['gettime'] = date('Y-m-d H:i:s',$value['gettime']);    
                    }else{
                    $list[$key]['gettime']="";   
                    }
                }
                $total = pdo_fetchcolumn("SELECT count(*) FROM " . tablename('ewei_shop_commission_gold_log') . " WHERE uniacid = '{$_W['uniacid']}' ORDER BY id DESC");
                $pager = pagination($total, $pindex, $psize);
        }
        if(!empty($_GPC['refund'])){
            $log=pdo_fetch('select * from '.tablename('ewei_shop_commission_gold_log')."where uniacid = '{$_W['uniacid']}' and id='{$_GPC['refund']}' ");
            $user=pdo_fetch('select * from '.tablename('ewei_shop_member')." where uniacid = '{$_W['uniacid']}' and id='{$log['mid']}'");
            $mc_user=pdo_fetch('select * from '.tablename('mc_members')." where uniacid = '{$_W['uniacid']}' and uid='{$user['uid']}'");
            $gold=$mc_user['gold']+$log['money'];
            pdo_update('mc_members', array('gold'=>$gold),array('uid'=>$user['uid']));
            pdo_update('ewei_shop_commission_gold_log', array('status'=>2,'refundtime'=>time()),array('id'=>$_GPC['refund']));
            $msg = array(
                'first' => array('value' => "金币退还通知！", "color" => "#4a5077"),
                'money' => array('title' => '退还金额', 'value' => '￥' . $log['money'] . '元', "color" => "#4a5077"),
                'time' => array('title' => '退还时间', 'value' =>date('Y-m-d H:i:s',time()), "color" => "#4a5077"),
                'remark' => array('value' => "\r\n请在“分销中心”中查收！谢谢您对小妖家的支持！", "color" => "#4a5077")
            );
            m('message')->sendCustomNotice($user['openid'], $msg);
            message('退还金币成功！',$this->createPluginWebUrl('goldshop/give_log'), 'success');
        }
	if ($_GPC['export'] == 1) {
            foreach ($list as $key => $value){
                    if($value['status']==1){
                     $list[$key]['status']="已领取";   
                    }elseif($value['status']==2){
                     $list[$key]['status']="已退回";   
                    }else{
                     $list[$key]['status']="未领取";      
                    }
            }
		$columns = array(
                    array('title' => 'ID', 'field' => 'id', 'width' => 12),
                    array('title' => '创建时间', 'field' => 'createtime', 'width' => 12), 
                    array('title' => '领取时间', 'field' => 'gettime', 'width' => 12),
                    array('title' => "创建人呢称", 'field' => 'nickname', 'width' => 24),
                    array('title' => '创建人姓名', 'field' => 'realname', 'width' => 12),
                    array('title' => '领取人呢称', 'field' => 'nickname2', 'width' => 12),
                    array('title' => '领取人姓名', 'field' => 'realname2', 'width' => 12),
                    array('title' => '金额', 'field' => 'money', 'width' => 12),
                    array('title' => '是否领取', 'field' => 'status', 'width' =>50)
               );
                m('excel')->export($list, array("title" => "金币赠送记录导出" . date('Y-m-d-H-i', time()), "columns" => $columns));
	}
}
load()->func('tpl');
include $this->template('give_log');